package mqtts

import (
	"gitee.com/ymofen/gobase"
	"gitee.com/ymofen/golog"
	"github.com/spf13/cobra"
	"time"
)

func init() {
	gobase.ArgFuncRegister("mqtts", gobase.ArgFuncOption{Catalog: "corbaRoot", Func: func(args string, tag interface{}) (val interface{}, err error) {
		var cbrunner CorbaRunner
		var addCorbaTask = &cobra.Command{
			Use: "mqtts",
			Run: func(cmd *cobra.Command, args []string) {
				golog.Infof("addr:%s", cbrunner.Addr)
				golog.Infof("wsport:%s", cbrunner.WsPort)
				golog.Infof("wspath:%s", cbrunner.WsPath)
				cbrunner.Run()
				gobase.HookExitSignalAndInterval(0, time.Second*10, func() {

				})
			},
		}
		addCorbaTask.Flags().StringVarP(&cbrunner.Addr, "addr", "a", "0.0.0.0:1883", "")
		addCorbaTask.Flags().StringVarP(&cbrunner.WsPort, "wsport", "w", "", "")
		addCorbaTask.Flags().StringVarP(&cbrunner.WsPath, "wspath", "", "", "")
		return addCorbaTask, nil
	}})

}

type CorbaRunner struct {
	Id   string
	Addr string

	WsPort string
	WsPath string
}

func (this *CorbaRunner) Run() error {
	if len(this.Id) == 0 {
		this.Id = "MqttS"
	}

	ms := &MqttServ{}
	ms.addr = this.Addr
	ms.wsport = this.WsPort
	ms.wspath = this.WsPath
	ms.Start()

	return nil
}
